/* ===================

AX2E (AXESCON XACML 2.0 Engine) is the Java authorization engine, which implements OASIS XACML 2.0 standard.
Copyright (C) 2007 AXESCON LLC

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


Contact: AXESCON LLC - info{at}axescon.com

 =================== */
package axescon.xacml.xb.ctx;

import axescon.xacml.model.policy.GenericMatch;
import axescon.xacml.model.policy.PolicyObject;
import axescon.xacml.model.policy.Target;
import axescon.xacml.model.policy.TargetPart;
import axescon.xacml.model.policy.impl.*;
import x0PolicySchemaOs.oasisNamesTcXacml2.*;

/**
 * @author Date: Sep 22, 2005
 *         Responsibilities:
 */
public class XBPolicyTargetFactory {
    public Target getTarget(TargetType xbtt, PolicyObject parent) throws Exception {
        TargetImpl ret = new TargetImpl();
        ret.setParentPolicyObject(parent);
        if (xbtt.isSetActions())
            ret.setActionArray(getActionArray(xbtt.getActions().getActionArray(), parent));
        if (xbtt.isSetEnvironments())
            ret.setEnvironmentArray(getEnvironmentArray(xbtt.getEnvironments().getEnvironmentArray(), parent));
        if (xbtt.isSetSubjects())
            ret.setSubjectArray(getSubjectArray(xbtt.getSubjects().getSubjectArray(), parent));
        if (xbtt.isSetResources())
            ret.setResourceArray(getResourceArray(xbtt.getResources().getResourceArray(), parent));

        return ret;
    }


    private GenericMatch[] getActionMatchArray(ActionMatchType[] atl, PolicyObject root) throws Exception {
        GenericMatch[] ret = new GenericMatch[atl.length];
        for (int i = 0; i < atl.length; i++) {
            ret[i] = getActionMatch(atl[i], root);
        }
        return ret;
    }


    private TargetPart[] getActionArray(ActionType[] atl, PolicyObject parent) throws Exception {
        TargetPart[] ret = new TargetPart[atl.length];
        for (int i = 0; i < atl.length; i++) {
            ret[i] = getAction(atl[i], parent);
        }
        return ret;
    }

    public TargetPart getAction(ActionType xbat, PolicyObject parent) throws Exception {
        if (xbat == null) return null;

        TargetPartImpl ret = new TargetPartImpl();
        ret.setParentPolicyObject(parent);

        if (xbat.sizeOfActionMatchArray() > 0) ret.setMatchArray(getActionMatchArray(xbat.getActionMatchArray(), parent));

        return ret;
    }


    public GenericMatch getActionMatch(ActionMatchType xbat, PolicyObject parent) throws Exception {
        if (xbat == null) return null;

        GenericMatchImpl ret = new GenericMatchImpl();
        ret.setParentPolicyObject(parent);
        ret.setAttributeValue(getXbAttrValueFactoryManager().getAttributeValue(xbat.getAttributeValue(), parent));
        ret.setMatchId(xbat.getMatchId());
        if (xbat.isSetActionAttributeDesignator())
            ret.setAttributeDesignator(getXbAttributeDesignatorFactory().getAttributeDesignator(new ActionAttributeDesignatorImpl(), xbat.getActionAttributeDesignator(), parent));
        else
            ret.setAttributeSelector(getXbAttributeSelectorFactory().getAttributeSelector
                    (xbat.getAttributeSelector(), parent));

        return ret;
    }


    private TargetPart[] getResourceArray(ResourceType[] atl, PolicyObject parent) throws Exception {
        TargetPart[] ret = new TargetPart[atl.length];
        for (int i = 0; i < atl.length; i++) {
            ret[i] = getResource(atl[i], parent);
        }
        return ret;
    }

    public TargetPart getResource(ResourceType xbat, PolicyObject parent) throws Exception {
        if (xbat == null) return null;

        TargetPartImpl ret = new TargetPartImpl();
        ret.setParentPolicyObject(parent);

        if (xbat.sizeOfResourceMatchArray() > 0) ret.setMatchArray(getResourceMatchArray(xbat.getResourceMatchArray(), parent));
        return ret;
    }

    private GenericMatch[] getResourceMatchArray(ResourceMatchType[] atl, PolicyObject parent) throws Exception {
        GenericMatch[] ret = new GenericMatch[atl.length];
        for (int i = 0; i < atl.length; i++) {
            ret[i] = getResourceMatch(atl[i], parent);
        }
        return ret;
    }

    public GenericMatch getResourceMatch(ResourceMatchType xbat, PolicyObject parent) throws Exception {
        if (xbat == null) return null;

        GenericMatchImpl ret = new GenericMatchImpl();
        ret.setParentPolicyObject(parent);
        ret.setMatchId(xbat.getMatchId());
        ret.setAttributeValue(getXbAttrValueFactoryManager().getAttributeValue(xbat.getAttributeValue(), parent));
        if (xbat.isSetResourceAttributeDesignator())
            ret.setAttributeDesignator(getXbAttributeDesignatorFactory().getAttributeDesignator(new ResourceAttributeDesignatorImpl(), xbat.getResourceAttributeDesignator(), parent));
        else
            ret.setAttributeSelector(getXbAttributeSelectorFactory().getAttributeSelector
                    (xbat.getAttributeSelector(), parent));

        return ret;
    }

    private TargetPart[] getEnvironmentArray(EnvironmentType[] atl, PolicyObject parent) throws Exception {
        TargetPart[] ret = new TargetPart[atl.length];
        for (int i = 0; i < atl.length; i++) {
            ret[i] = getEnvironment(atl[i], parent);
        }
        return ret;
    }

    public TargetPart getEnvironment(EnvironmentType xbat, PolicyObject parent) throws Exception {
        if (xbat == null) return null;

        TargetPartImpl ret = new TargetPartImpl();
        ret.setParentPolicyObject(parent);

        if (xbat.sizeOfEnvironmentMatchArray() > 0) ret.setMatchArray(getEnvironmentMatchArray(xbat.getEnvironmentMatchArray(), parent));

        return ret;
    }

    private GenericMatch[] getEnvironmentMatchArray(EnvironmentMatchType[] atl, PolicyObject parent) throws Exception {
        GenericMatch[] ret = new GenericMatch[atl.length];
        for (int i = 0; i < atl.length; i++) {
            ret[i] = getEnvironmentMatch(atl[i], parent);
        }
        return ret;
    }

    public GenericMatch getEnvironmentMatch(EnvironmentMatchType xbat, PolicyObject parent) throws Exception {
        if (xbat == null) return null;

        GenericMatchImpl ret = new GenericMatchImpl();
        ret.setParentPolicyObject(parent);
        ret.setMatchId(xbat.getMatchId());
        ret.setAttributeValue(getXbAttrValueFactoryManager().getAttributeValue(xbat.getAttributeValue(), parent));
        if (xbat.isSetEnvironmentAttributeDesignator())
            ret.setAttributeDesignator(getXbAttributeDesignatorFactory().getAttributeDesignator(new EnvironmentAttributeDesignatorImpl(), xbat.getEnvironmentAttributeDesignator(), parent));
        else
            ret.setAttributeSelector(getXbAttributeSelectorFactory().getAttributeSelector
                    (xbat.getAttributeSelector(), parent));

        return ret;
    }

    private TargetPart[] getSubjectArray(SubjectType[] atl, PolicyObject parent) throws Exception {
        TargetPart[] ret = new TargetPart[atl.length];
        for (int i = 0; i < atl.length; i++) {
            ret[i] = getSubject(atl[i], parent);
        }
        return ret;
    }

    public TargetPart getSubject(SubjectType xbat, PolicyObject parent) throws Exception {
        if (xbat == null) return null;

        TargetPartImpl ret = new TargetPartImpl();
        ret.setParentPolicyObject(parent);

        if (xbat.sizeOfSubjectMatchArray() > 0) ret.setMatchArray(getSubjectMatchArray(xbat.getSubjectMatchArray(), parent));

        return ret;
    }

    private GenericMatch[] getSubjectMatchArray(SubjectMatchType[] atl, PolicyObject parent) throws Exception {
        GenericMatch[] ret = new GenericMatch[atl.length];
        for (int i = 0; i < atl.length; i++) {
            ret[i] = getSubjectMatch(atl[i], parent);
        }
        return ret;
    }

    public GenericMatch getSubjectMatch(SubjectMatchType xbat, PolicyObject parent) throws Exception {
        if (xbat == null) return null;

        GenericMatchImpl ret = new GenericMatchImpl();
        ret.setParentPolicyObject(parent);
        ret.setMatchId(xbat.getMatchId());
        ret.setAttributeValue(getXbAttrValueFactoryManager().getAttributeValue(xbat.getAttributeValue(), parent));
        if (xbat.isSetSubjectAttributeDesignator())
            ret.setAttributeDesignator(getXbAttributeDesignatorFactory().getSubjectAttributeDesignator(xbat.getSubjectAttributeDesignator(), parent));
        else
            ret.setAttributeSelector(getXbAttributeSelectorFactory().getAttributeSelector
                    (xbat.getAttributeSelector(), parent));

        return ret;
    }


    private XBAttributeDesignatorFactory xbAttributeDesignatorFactory = new XBAttributeDesignatorFactory();

    public XBAttributeDesignatorFactory getXbAttributeDesignatorFactory() {
        return xbAttributeDesignatorFactory;
    }

    public void setXbAttributeDesignatorFactory(XBAttributeDesignatorFactory xbAttributeDesignatorFactory) {
        this.xbAttributeDesignatorFactory = xbAttributeDesignatorFactory;
    }

    private XBAttrValueFactoryManager xbAttrValueFactoryManager = new XBAttrValueFactoryManager();

    public XBAttrValueFactoryManager getXbAttrValueFactoryManager() {
        return xbAttrValueFactoryManager;
    }

    public void setXbAttrValueFactoryManager(XBAttrValueFactoryManager xbAttrValueFactoryManager) {
        this.xbAttrValueFactoryManager = xbAttrValueFactoryManager;
    }


    private XBAttributeSelectorFactory xbAttributeSelectorFactory = new XBAttributeSelectorFactory();

    public XBAttributeSelectorFactory getXbAttributeSelectorFactory() {
        return xbAttributeSelectorFactory;
    }

    public void setXbAttributeSelectorFactory(XBAttributeSelectorFactory xbAttributeSelectorFactory) {
        this.xbAttributeSelectorFactory = xbAttributeSelectorFactory;
    }
}
